<?php

class Ads_Model_City {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Ads_Model_DbTable_City();
    }

    public function fetchAll() {
        $select = $this->_dbtable->select();
        return $this->_dbtable->fetchAll($select);
    }

    public function getCityById($id) {
        $select = $this->_dbtable->select();
        $select->where("id = ? ", $id);
        return $this->_dbtable->fetchAll($select);
    }

    public function getIdByCity($city) {
        $select = $this->_dbtable->select();
        $select->where("name = ? ", $city);
        return $this->_dbtable->fetchAll($select);
    }

    public function updateCity($data, $id) {
        $this->_dbtable->update($data, "id = $id");
    }

    public function insert($data) {
        return $this->_dbtable->insert($data);
    }

    public function getStateNameFromStateId($stateId) {
        $db = new Ads_Model_State();
        return $db->getStateById($stateId);
    }

    public function getStateNameFromCityObject($city) {
        return $this->getStateNameFromStateId($city[0]['state_id']);
    }

    public function getStateNameFromCityId($cityId) {
        $city = $this->getCityById($cityId);
        return $this->getStateNameFromCityObject($city);
    }

    public function getCityStated($state_id = 0) {
        $select = $this->_dbtable->select();
        $select->where("state_id = ?", $state_id);
        return $this->_dbtable->fetchAll($select);
    }

}

